package com.hwadee.backend.controller;

import com.hwadee.backend.common.result.CommonResult;
import com.hwadee.backend.entity.HealthRecord.*;
import com.hwadee.backend.entity.BatchHealthRecordRequest;
import com.hwadee.backend.service.HealthRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/healthRecord")
public class HealthRecordController {
    @Autowired
    private HealthRecordService healthRecordService;
    private static final Logger logger = LoggerFactory.getLogger(HealthRecordController.class);
    // TODO: 这里应该返回一个json格式的健康记录
    //后端需要token验证、前端需要给后端用户名、要查的档案种类
    // 后端通过id查询recordId再跟俊recordId和recordType查询

    @GetMapping("/getAllergyHistory/{userId}")
    public CommonResult<List<AllergyHistory>> getAllergyHistory(@PathVariable int userId) {
        logger.info("查询过敏史，用户ID: {}", userId);
        try {
            List<AllergyHistory> result = healthRecordService.getAllergyHistory(userId);
            return CommonResult.success(result);
        } catch (Exception e) {
            logger.error("查询过敏史失败，用户ID: {}", userId, e);
            return CommonResult.error(500, "查询过敏史失败");
        }
    }

    @GetMapping("/getFamilyHistory/{userId}")
    public CommonResult<List<FamilyHistory>> getFamilyHistory(@PathVariable int userId) {
        logger.info("查询家族病史，用户ID: {}", userId);
        try {
            List<FamilyHistory> result = healthRecordService.getFamilyHistory(userId);
            return CommonResult.success(result);
        } catch (Exception e) {
            logger.error("查询家族病史失败，用户ID: {}", userId, e);
            return CommonResult.error(500, "查询家族病史失败");
        }
    }

    @GetMapping("/getHealthIndicators/{userId}")
    public CommonResult<List<HealthIndicators>> getHealthIndicators(@PathVariable int userId) {
        logger.info("查询健康指标，用户ID: {}", userId);
        try {
            List<HealthIndicators> result = healthRecordService.getHealthIndicators(userId);
            return CommonResult.success(result);
        } catch (Exception e) {
            logger.error("查询健康指标失败，用户ID: {}", userId, e);
            return CommonResult.error(500, "查询健康指标失败");
        }
    }

    @GetMapping("/getMedicalExamReport/{userId}")
    public CommonResult<List<MedicalExamReport>> getMedicalExamReport(@PathVariable int userId) {
        logger.info("查询体检报告，用户ID: {}", userId);
        try {
            List<MedicalExamReport> result = healthRecordService.getMedicalExamReport(userId);
            return CommonResult.success(result);
        } catch (Exception e) {
            logger.error("查询体检报告失败，用户ID: {}", userId, e);
            return CommonResult.error(500, "查询体检报告失败");
        }
    }

    @GetMapping("/getMedicationRecord/{userId}")
    public CommonResult<List<MedicationRecord>> getMedicationRecord(@PathVariable int userId) {
        logger.info("查询用药记录，用户ID: {}", userId);
        try {
            List<MedicationRecord> result = healthRecordService.getMedicationRecord(userId);
            return CommonResult.success(result);
        } catch (Exception e) {
            logger.error("查询用药记录失败，用户ID: {}", userId, e);
            return CommonResult.error(500, "查询用药记录失败");
        }
    }

    @GetMapping("/getPastMedicalHistory/{userId}")
    public CommonResult<List<PastMedicalHistory>> getPastMedicalHistory(@PathVariable int userId) {
        logger.info("查询既往病史，用户ID: {}", userId);
        try {
            List<PastMedicalHistory> result = healthRecordService.getPastMedicalHistory(userId);
            return CommonResult.success(result);
        } catch (Exception e) {
            logger.error("查询既往病史失败，用户ID: {}", userId, e);
            return CommonResult.error(500, "查询既往病史失败");
        }
    }

    @PostMapping("/addHealthRecord/{id}")
    public CommonResult<Integer> addHealthRecord(@RequestBody HealthRecord healthRecord, @PathVariable int id) {
        logger.info("新增健康记录，类型: {}", healthRecord.getRecordType());
        try {
            int recordId = healthRecordService.addHealthRecord(healthRecord,id);
            return CommonResult.success(recordId);
        } catch (Exception e) {
            logger.error("新增健康记录失败，类型: {}", healthRecord.getRecordType(), e);
            return CommonResult.error(500, "新增健康记录失败");
        }
    }

    @DeleteMapping("/deleteHealthRecord/{recordId}")
    public CommonResult<Integer> deleteHealthRecord(@PathVariable int recordId) {
        logger.info("删除健康记录，记录 ID: {}", recordId);
        try {
            int result = healthRecordService.deleteHealthRecord(recordId);
            return CommonResult.success(result);
        } catch (Exception e) {
            logger.error("删除健康记录失败，记录 ID: {}", recordId, e);
            return CommonResult.error(500, "删除健康记录失败");
        }
    }
    @PutMapping("/updateHealthRecord")
    public CommonResult<Integer> updateHealthRecord(@RequestBody HealthRecord healthRecord) {
        logger.info("更新健康记录，记录 ID: {}", healthRecord.getRecordId());
        try {
            int result = healthRecordService.updateHealthRecord(healthRecord);
            return CommonResult.success(result);
        }
        catch (Exception e) {
            logger.error("更新健康记录失败，记录 ID: {}", healthRecord.getRecordId(), e);
            return CommonResult.error(500, "更新健康记录失败");
        }
    }

    @PostMapping("/batchInsert")
    public CommonResult<Boolean> batchInsertHealthRecords(@RequestBody BatchHealthRecordRequest request) {
        logger.info("批量插入健康记录，用户 ID: {}", request.getUserId());
        try {
            boolean result = healthRecordService.batchInsertHealthRecords(request);
            return CommonResult.success(result);
        } catch (Exception e) {
            logger.error("批量插入健康记录失败", e);
            return CommonResult.error(500, "批量插入失败");
        }
    }
}
